package com.mysteel.cart.vo;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ProjectName: mail
 * ClassName: Cart
 * Description:
 * date: 2021/03/15/015 13:43
 *
 * @author xiaochao
 */
public class Cart {
    private List<CartItem> cartItems;
    /**
     * 商品数量
     */
    private Integer countNum;
    /**
     * 商品类型
     */
    private Integer countType;
    /**
     * 商品总价
     */
    private BigDecimal amount;
    /**
     * 减免价格
     */
    private BigDecimal reduce = new BigDecimal(0);

    /**
     * 所有skuId
     */
    private String shuIds;

    /**
     *
     */
    private Boolean allCheck;

    public List<CartItem> getCartItems() {
        return cartItems;
    }

    public void setCartItems(List<CartItem> cartItems) {
        this.cartItems = cartItems;
    }

    public Integer getCountNum() {
        int count = 0;
        if (!CollectionUtils.isEmpty(cartItems)){
            for (CartItem cartItem : cartItems) {
                count += cartItem.getCount();
            }
        }
        return count;
    }


    public Integer getCountType() {
        int count = 0;
        if (!CollectionUtils.isEmpty(cartItems)){
            count = cartItems.size();
        }
        return count;
    }


    public BigDecimal getAmount() {
        BigDecimal amount = new BigDecimal(0);
        // 计算购物项总价
        if (!CollectionUtils.isEmpty(cartItems)){
            for (CartItem cartItem : cartItems) {
                if (cartItem.getCheck()){
                    BigDecimal totalPrice = cartItem.getTotalPrice();
                    amount = amount.add(totalPrice);
                }
            }
        }
        // 减去优惠总价
        amount = amount.subtract(this.reduce);
        return amount;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }

    public String getShuIds() {
        String skuIds = "";
        if (!CollectionUtils.isEmpty(cartItems)){
            List<String> collect = cartItems.stream().map(item -> item.getSkuId().toString()).collect(Collectors.toList());
            skuIds = String.join(",",collect);
        }
        return skuIds;
    }

    public Boolean getAllCheck() {
        if (!CollectionUtils.isEmpty(cartItems)){
            return cartItems.stream().allMatch(CartItem::getCheck);
        }
        return false;
    }

    public void setAllCheck(Boolean allCheck) {
        this.allCheck = allCheck;
    }
}
